class PEntriesController < ApplicationController
  before_action :set_p_entry, only: [:show, :edit, :update, :destroy]

public 
  def viewdes
    @pp = "teststringshow"
    tokenid = cookies[:token]


    client = TrackerApi::Client.new(token: tokenid) 



    #project  = client.project(1121980)         # Find project with given ID
     @projects = client.projects
     #projects.each do |p|
     #    @pname=p.name
     #end



   # PivotalTracker::Client.token = '0e8a2bbff4a6c292f81d14dfde98a2c8'
  end


  # GET /p_entries
  # GET /p_entries.json
  def index
    #cookies[:token]="0e8a2bbff4a6c292f81d14dfde98a2c8" #kami's token
     cookies[:token]="907a8e2a34bdd3299613a1ae64776498" #jeremy's token
    # para = JSON.parse(params[:date_pick_params].to_s) rescue {}
    # para=JSON.parse(params[:date_pick_params].to_s.to_json)
    date=params[:date]


    ar=date.to_s.split(" ")


    # puts "indexhere:"
    # puts date
    # puts ar[1]
    # puts ar[2]
    # puts ar[3]

    year=ar[3].to_s
    day=ar[2].to_s
    month=ar[1].to_s
    puts month
    case month.to_s
    when "Jan"
      mon="01"
    when "Feb"
      mon="02"
    when "Mar"
      mon="03"
    when "Apr"
      mon="04"
    when "May"
      mon="05"
    when "Jun"
      mon="06"
    when "Jul"
      mon="07"
    when "Aug"
      mon="08"
    when "Sep"
      mon="09"
    when "Oct"
      mon="10"
    when "Nov"
      mon="11"
    when "Dec"
      mon="12"
    else
      mon=""
    end

     ssdate=year.to_s+'-'+mon.to_s+'-'+day.to_s
     puts "ssdate:",ssdate

    @sdate2=mon+"/"+day+"/"+year
    # sdate=""
    # puts params[:date_pick_params].to_s.to_json
   

    token =cookies[:token].to_s

    
    @odtype = params[:ordertype].to_s
    @odtype +=",project,story asc"
    puts "ordertype", @odtype
    @p_entries = PEntry.where("token = ? AND date = ?",token,ssdate).order(@odtype)
    # puts "p_entries.size:",@p_entries.size
    # @p_entries = PEntry.all

    @pp="123"
    respond_to do |format|
      format.html
      format.js    
      format.json  do
        render(json:@p_entries)
      end

    end

    

  end

  # GET /p_entries/1
  # GET /p_entries/1.json
  def show
    puts "showstart"
  end

  def showpartial

     @test = params[:param1]
     puts 123
     puts @test


    
  end
  # GET /p_entries/new
  def new
    @p_entry = PEntry.new
  end

  # GET /p_entries/1/edit
  def edit
  end

  # POST /p_entries
  # POST /p_entries.json
  def create
    puts "create start"
    @p_entry = PEntry.new(p_entry_params)

    tokenid=cookies[:token].to_s
    puts "tokenid:",tokenid
    client = TrackerApi::Client.new(token: tokenid) 

    str=params[:p_entry][:ptracker].to_s
    story=client.story(str)
    project = client.project(story.project_id)
    
   # project  = client.project(1121980)         # Find project with given ID
    @p_entry.token=tokenid
    @p_entry.projectid=story.project_id
    @p_entry.project=project.name
    @p_entry.story=story.name
    @p_entry.storytype=story.story_type
    @p_entry.storydescription=story.description
    @p_entry.date=Date.today.to_formatted_s(:db).to_s

   # params[:task][:assignmentId]
    respond_to do |format|
      if @p_entry.save
        format.html { redirect_to @p_entry, notice: 'P entry was successfully created.' }
        format.json { render :show, status: :created, location: @p_entry }
        


      else
        format.html { render :new }
        format.json { render json: @p_entry.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /p_entries/1
  # PATCH/PUT /p_entries/1.json
  def update
    puts "update start"
    respond_to do |format|
      if @p_entry.update(p_entry_params)
        format.html { redirect_to @p_entry, notice: 'P entry was successfully updated.' }
        format.json { render :show, status: :ok, location: @p_entry }
      else
        format.html { render :edit }
        format.json { render json: @p_entry.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /p_entries/1
  # DELETE /p_entries/1.json
  def destroy
    puts "destroy start"
    @p_entry.destroy
    respond_to do |format|
      format.html { redirect_to @p_entry, notice: 'P entry was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_p_entry
      @p_entry = PEntry.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def p_entry_params
      params.require(:p_entry).permit(:project, :pe_type, :ptracker, :status, :note)
    end
end
